package com.zhang.network.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.dto.BaseResponse;
import com.zhang.network.constant.BizResponse;
import com.zhang.network.model.*;
import com.zhang.network.model.Friend;
import com.zhang.network.model.friend.FriendQueryDTO;
import com.zhang.network.model.friend.FriendVO;
import com.zhang.network.service.FriendService;

import com.zhang.network.service.UserService;
import com.zhang.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/friend")
public class FriendController {

    @Resource
    FriendService friendService;

    @Resource
    UserService userService;

    @GetMapping("/page")
    public BaseResponse<Page<FriendVO>> page(FriendQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        Page<FriendVO> friendPage = friendService.pageFriend(queryDTO);
        if (friendPage.getRecords().size() > 0) {
            return BizResponse.success(friendPage);
        }
        return BizResponse.warn("未查询到相关人脉");
    }

    @GetMapping("/getFriends")
    public BaseResponse getFriends(FriendQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        List<Friend> friends = friendService.list(new QueryWrapper<Friend>().eq("user_id", queryDTO.getId()));
        if (friends.size() <= 0) {
            return BizResponse.warn("未查询到相关人脉");
        }
        List<Integer> list1 = friends.stream().map(friend -> friend.getFriendId()).collect(Collectors.toList());
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.in("id", list1);
        if (StringUtils.isNotBlank(queryDTO.getNickName())) {
            userWrapper.like("nick_name", queryDTO.getNickName());
        }
        if (StringUtils.isNotBlank(queryDTO.getProfession())) {
            userWrapper.like("profession", queryDTO.getProfession());
        }
        if (StringUtils.isNotBlank(queryDTO.getCity())) {
            userWrapper.like("city", queryDTO.getCity());
        }
        if (StringUtils.isNotBlank(queryDTO.getAccount())) {
            userWrapper.like("account", queryDTO.getAccount());
        }
        List<User> users = userService.list(userWrapper);

        return BizResponse.success(users);
    }


    @GetMapping("/pageRank")
    public BaseResponse<Page<FriendVO>> pageRank(FriendQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        Page<FriendVO> friendPage = friendService.pageRankFriend(queryDTO);
        if (friendPage.getRecords().size() > 0) {
            return BizResponse.success(friendPage);
        }
        return BizResponse.warn("未查询到相关人脉");
    }

    @GetMapping("/getRank")
    public BaseResponse getRank(Friend friend) {
        List<Friend> list = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", friend.getUserId())
                .eq("friend_Id", friend.getFriendId()));
        if (list.size() > 0) {
            friend = list.get(0);
            FriendVO friendVO = new FriendVO();
            friendVO.setRank(friend.getRank());
            return BizResponse.success(friendVO);
        }
        return BizResponse.warn("未查询到相关人脉等级");
    }

    @GetMapping("/getSameNetwork")
    public BaseResponse getSameNetwork(Friend friend) {
        List<Network> list = friendService.getSameNetwork(friend);
        if (list.size() > 0) {
            return BizResponse.success(list);
        }
        return BizResponse.warn("无共同人脉圈");
    }

    @GetMapping("/add")
    public BaseResponse add(Friend friend) {
        log.info("请求参数：{}", friend.toString());
        List<Friend> list = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", friend.getUserId())
                .eq("friend_Id", friend.getFriendId()));
        if (list.size() > 0) {
            return BizResponse.warn("已拥有此人脉");
        }
        Friend newFriend = new Friend();
        newFriend.setFriendId(friend.getUserId());
        newFriend.setUserId(friend.getFriendId());
        boolean result = friendService.save(friend);
        boolean result2 = friendService.save(newFriend);
        if (result && result2) {
            return BizResponse.success();
        }
        return BizResponse.fail("添加失败");
    }

    @GetMapping("/delete")
    public BaseResponse<Page<User>> delete(Friend friend) {
        log.info("请求参数：{}", friend.toString());
        boolean result = friendService.deleteFriend(friend);

        Friend newFriend = new Friend();
        newFriend.setFriendId(friend.getUserId());
        newFriend.setUserId(friend.getFriendId());
        boolean result2 = friendService.deleteFriend(newFriend);
        if (result && result2) {
            return BizResponse.success();
        }
        return BizResponse.fail("删除失败");
    }

    @GetMapping("/check")
    public BaseResponse<Page<User>> check(Friend friend) {
        log.info("请求参数：{}", friend.toString());
        List<Friend> list = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", friend.getUserId())
                .eq("friend_Id", friend.getFriendId()));
        if (list.size() > 0) {
            return BizResponse.warn("已拥有此人脉");
        }
        return BizResponse.success();
    }

}

